$(function(){
    var page=1;
    var fi =1;
    var pagenum=10;
	var tddata=[
		{
			key:'order_no',
			type:1,
			url:''
		},{
			key:'order_type2',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'order_state2',
			type:1,
			url:''
		},{
			key:'orderer',
			type:1,
			url:''
		},{
			key:'order_time',
			type:1,
			url:''
		},{
			key:'passenger_name',
			type:1,
			url:''
		},{
			key:'passenger_phone',
			type:1,
			url:''
		},{
			key:'company_name',
			type:1,
			url:''
		},{
			key:'from_addr',
			type:1,
			url:''
		},{
			key:'to_addr',
			type:1,
			url:''
		},{
			key:'approval',
			type:1,
			url:''
		},{
			key:'driver_name',
			type:1,
			url:''
		},{
			key:'car_number',
			type:1,
			url:''
		},{
			key:'car_type_name',
			type:1,
			url:''
		},{
			key:'use_explain',
			type:1,
			url:''
		},{
			key:'order_money',
			type:1,
			url:''
		},{
			key:'meter',
			type:1,
			url:''
		},{
			key:'use_time',
			type:1,
			url:''
		},{
			key:'apply_time',
			type:1,
			url:''
		},{
			key:'approval_time',
			type:1,
			url:''
		},{
			key:'orders_time',
			type:1,
			url:''
		},{
			key:'ride_time',
			type:1,
			url:''
		}];
	
	var addoperation=``;
	
	var data={
		page:page,
		pagenum:10
	}
	laydate.render({
        elem: '#between'
        ,range: true
      });
    var where_json={}
    console.log($('.orderertype').val())
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}
	{if $Think.get.name}
		if($('.orderertype').val()==1){
			where_json.passenger_name=['like','%{$Think.get.name}%']
		}else if($('.orderertype').val()==2){
			where_json.orderer=['like','%{$Think.get.name}%']
		}
		
	{/if}
	{if $Think.get.passenger_phone}
		where_json.passenger_phone=['like','%{$Think.get.passenger_phone}%']
	{/if}
	{if $Think.get.between}
	var start = '{$Think.get.between}'.split(' ');
	console.log(start)
    where_json.orders_time=['between time',[start[0],start[2]]]
	{/if}
	data.where_json=JSON.stringify(where_json)
	ajaxorder(data)
	function ajaxorder(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:api.order.list,
			success:function(res){
				Load(1)
				console.log(res)
				res=orderstate(res)
				if(res.code==1){
				console.log(res.data)
				 table.render({
					    	elem: '#order'
						    ,cols: [[ //标题栏
						      {field: 'order_no', title: '订单号', width: 250, sort: true,fixed: 'left'}
						      ,{field: 'order_type2', title: '订单类型', width: 150}
						      ,{field: 'order_state2', title: '订单状态', minWidth: 150}
						      ,{field: 'orderer', title: '下单人', minWidth: 150}
						      ,{field: 'order_time', title: '下单时间', width: 180}
						      ,{field: 'passenger_name', title: '乘客', minWidth: 150}
						      ,{field: 'passenger_phone', title: '乘客电话', minWidth: 150}
						      ,{field: 'company_name', title: '乘车单位', width: 150,templet: `<div>{{d.company.company_name?d.company.company_name:'-'}}</div>`}
						      ,{field: 'from_addr', title: '出发地', minWidth: 150 ,templet: `<div>{{d.gps.to_addr?d.gps.from_addr:'-'}}</div>`}
						      ,{field: 'to_addr', title: '目的地', minWidth: 150,templet: `<div>{{d.gps.to_addr?d.gps.to_addr:'-'}}</div>`}
						      ,{field: 'approval', title: '审批人', width: 150}
						      ,{field: 'driver_name', title: '司机', minWidth: 150}
						      ,{field: 'car_number', title: '车牌', minWidth: 150}
						      ,{field: 'car_type_name', title: '车型', width: 150}
						      ,{field: 'use_explain', title: '用车说明', minWidth: 150}
						      ,{field: 'order_money', title: '订单费用', minWidth: 150,templet: `<div>{{d.fee.order_money?d.fee.order_money:'-'}}</div>`}
						      ,{field: 'meter', title: '订单里程', minWidth: 150,templet: `<div>{{d.fee.meter?d.fee.meter:'-'}}</div>`}
						      ,{field: 'use_time', title: '用时', minWidth: 150,templet: `<div>{{d.fee.use_time?(d.fee.use_time/3600).toFixed(2)+'小时':'-'}}</div>`}
						      ,{field: 'apply_time', title: '申请时间', width: 180}
						      ,{field: 'approval_time', title: '批准时间', width: 180}
						      ,{field: 'orders_time', title: '接单时间', width: 180}
						      ,{field: 'ride_time', title: '乘车时间', width: 180}
						    ]]
                            ,data:res.data,
                            cellMinWidth: 80
                            ,limit: data.pagenum
						    ,even: true
				
					  	});
					
					  //展示已知数据
					 	
					//   分页
                          if (fi == 1) {
                            laypage.render({
                                elem: 'page',
                                count: res.count,
								layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
								theme: '#1E9FFF',
                                jump: function (obj, first) {
                                    console.log(obj)
                                    if (!first) {
                                        page = obj.curr;
                                        data.page = page;
                                        pagenum = obj.limit;
                                        data.pagenum = pagenum;
                                        ajaxorder(data);
                                    } else {
                
                                    }
                                }
							})
							$('.daochu').on('click',function(){
								var getkey="";
								for(var iii=0;iii<tddata.length;iii++){
									if(tddata[iii].key=='order_type2'){
										for(var key in res.description){
											if(key=='order_type'){
												tddata[iii].text=res.description[key].name
											}
										}
										getkey+=`<li>
												<label class="am-checkbox">
												  <input type="checkbox" value="" data-key='order_type' checked> ${tddata[iii].text}
												</label>
											</li>`
									}else if(tddata[iii].key=='order_state2'){
										for(var key in res.description){
											if(key=='order_state'){
												tddata[iii].text=res.description[key].name
											}
										}
										getkey+=`<li>
												<label class="am-checkbox">
												  <input type="checkbox" value="" data-key='order_state' checked> ${tddata[iii].text}
												</label>
											</li>`
									}else{
										for(var key in res.description){
											if(key==tddata[iii].key){
												tddata[iii].text=res.description[key].name
											}
										}
										getkey+=`<li>
												<label class="am-checkbox">
												  <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
												</label>
											</li>`
									}
									
								}
								layer.open({
									  type: 1,
									  title:false,
									  skin: 'layui-layer-rim', //加上边框
									  area: ['300px', '400px'], //宽高
									  content: `<div>
												 <div style='height:300px;overflow:auto;margin-bottom:30px'>
												  <ul class='daochuul' style="padding:10px;">${getkey}</ul>
												  </div>
												  <div style='text-align:center'>
												  <button class="layui-btn layui-btn-normal daochu_ok" type='button'>确定导出</button>
												  <button class='layui-btn layui-btn-danger am-radius' type='button'>取消导出</button>
												  </div>
											</div>`,
									success:function(){
										var ddata={}
										$('.daochu_ok').on('click',function(){
			//								window.open('{:url("/adminapi/Order/downLoadOrderList")}')
											ddata.fields="";
											var where_jsondao={};
											{if $Think.get.car_type_name}
												where_jsondao.car_type_name=['like','%{$Think.get.car_type_name}%']
											{/if}
											{if $Think.get.name}
												if($('.orderertype').val()==1){
													where_jsondao.passenger_name=['like','%{$Think.get.name}%']
												}else if($('.orderertype').val()==2){
													where_jsondao.orderer=['like','%{$Think.get.name}%']
												}
												
											{/if}
											{if $Think.get.passenger_phone}
												where_jsondao.passenger_phone=['like','%{$Think.get.passenger_phone}%']
											{/if}
											{if $Think.get.start_time}
												where_jsondao.orders_time=['between time',['{$Think.get.start_time}','{$Think.get.end_time}']]
											{/if}
											ddata.where_json=JSON.stringify(where_jsondao)
											
											$('.daochuul li input[type="checkbox"]').each(function(){
												if(this.checked){
													ddata.fields+=($(this).data('key')+",")
												}
											})
											ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
											console.log(ddata.fields)
											
											window.open(`${api.order.down}?where_json=${ddata.where_json}&fields=${ddata.fields}`)
			//								
										})
										
									}
								});
							})
                            fi = 2;
                        }
					
					
						
				
				
				

			}
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	}
})
$(function(){

})
